package com.gm.wj.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @program: wj
 * @description: 七牛云图片操作工具类
 * @author: Huang_shengjun
 * @create: 2020-08-17 21:32
 **/
@Slf4j
public class QiniuUtil {

    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "";
    private static final String SECRET_KEY = "";

    // 要上传的空间名称
    private static final String BUCKETNAME = "";
    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    // 外链默认域名
    private static final String DOMAIN = "http://";


    /**
     * 将照片、视频上传到七牛云
     */
    public static String uploadQN(MultipartFile file,String key) {

        // 构造一个带指定Zone对象的配置类, 注意这里的Zone.zone0需要根据主机选择  华南
        Configuration cfg = new Configuration(Zone.zone2());
        // 其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        // 生成上传凭证，然后准备上传

        try {
            String upToken = auth.uploadToken(BUCKETNAME);
            try {
                byte[] localFile = file.getBytes();
                Response response = uploadManager.put(localFile, key, upToken);
                // 解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

                String returnPath = DOMAIN + "/" + putRet.key;
                // 这个returnPath是获得到的外链地址,通过这个地址可以直接打开图片
                return returnPath;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println("2"+r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String uploadQNFile(MultipartFile file) {
        // 构造一个带指定Zone对象的配置类, 注意这里的Zone.zone0需要根据主机选择  华南
        Configuration cfg = new Configuration(Zone.zone2());
        // 其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        // 生成上传凭证，然后准备上传

        try {
            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
            String upToken = auth.uploadToken(BUCKETNAME);
            try {
                String originalFilename = file.getOriginalFilename();
                // 文件后缀
                String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                //文件名
                String key = StringUtils.generateOrderNo() + suffix;
                byte[] localFile = file.getBytes();
                Response response = uploadManager.put(localFile, key, upToken);
                // 解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

                String returnPath = DOMAIN + "/" + putRet.key;
                // 这个returnPath是获得到的外链地址,通过这个地址可以直接打开图片
                return returnPath;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
